package com.hgyc.mom.mes.controller;

import java.util.*;

import com.hgyc.mom.mes.entity.SopSendRecord;
import com.hgyc.mom.mes.vo.SopSendRecordParam;
import jakarta.servlet.http.HttpServletResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;
import com.hgyc.mom.mes.entity.SopDocumentManage;
import com.hgyc.mom.mes.vo.SopDocumentManageParam;
import com.hgyc.mom.mes.service.SopDocumentManageService;
import com.hgyc.mom.mes.vo.SopDocumentManageVO;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.common.util.poi.ExcelUtil;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 文档集Controller
 *
 * @author fwj
 * @since 2025-06-18
 */
@Tag(name = "文档集控制器", description = "文档集控制器")
@RestController
@RequestMapping("/mes/sop/manage")
public class SopDocumentManageController {
    @Autowired
    private SopDocumentManageService sopDocumentManageService;

    @Operation(summary = "查询文档集列表", description = "查询文档集列表")
    @PostMapping("/pageList")
    public Result<IPage<SopDocumentManage>> pageList(@Parameter(description = "查询参数") @RequestBody SopDocumentManageParam sopDocumentManageParam) {
        Page<SopDocumentManage> page = new Page<>(sopDocumentManageParam.getPage(), sopDocumentManageParam.getPageSize());
        SopDocumentManage sopDocumentManage = new SopDocumentManage();
        BeanUtils.copyProperties(sopDocumentManageParam, sopDocumentManage);
        QueryWrapper<SopDocumentManage> wrapper = QueryWrapperUtil.build(sopDocumentManage);
        IPage<SopDocumentManage> list = sopDocumentManageService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取文档集详细信息", description = "获取文档集详细信息")
    @GetMapping(value = "/{id}")
    public Result<?> getInfo(@Parameter(description = "文档集ID") @PathVariable("id") Long id) {
        return Result.success(sopDocumentManageService.getById(id));
    }
    @Operation(summary = "新增文档集", description = "新增文档集")
    @PostMapping
    public Result<?> save(@Parameter(description = "文档集") @RequestBody SopDocumentManageVO sopDocumentManageVO) {
        sopDocumentManageService.saveManage(sopDocumentManageVO);
        return Result.success();
    }

    @Operation(summary = "修改文档集", description = "修改文档集")
    @PutMapping
    public Result<?> update(@Parameter(description = "文档集") @RequestBody SopDocumentManageVO sopDocumentManageVO) {
        sopDocumentManageService.updateManage(sopDocumentManageVO);
        return Result.success();
    }

    @Operation(summary = "删除文档集", description = "删除文档集")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        sopDocumentManageService.removeManage(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "更新状态", description = "更新状态")
    @PutMapping("/status/{status}")
    public Result<?> updateStatus(@PathVariable("status") String status, @RequestParam("ids") Long[] ids) {
        sopDocumentManageService.updateStatus(status, Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "推送文档到指定终端", description = "推送文档到指定终端")
    @PostMapping("/sendDoc")
    public Result<?> sendDoc(@RequestBody List<SopSendRecord> sendRecords){
        sopDocumentManageService.sendMultipleDoc(sendRecords);
        return Result.success();
    }

    @Deprecated
    @Operation(summary = "根据终端IP获取文档", description = "根据终端IP获取文档(旧接口，不再使用)")
    @PostMapping("/getDocByIp")
    public Result<?> getDockByIp(String ip){
        return Result.success(sopDocumentManageService.getDocByIp(ip));
    }

    @Operation(summary = "根据终端IP获取文档，支持多个文档", description = "根据终端IP获取文档，支持多个文档")
    @GetMapping("/getMultipleDocByIp/{ip}")
    public Result<?> getMultipleDocByIp(@PathVariable("ip") String ip){
        return Result.success(sopDocumentManageService.getMultipleDocByIp(ip));
    }

    @Operation(summary = "根据终端IP获取终端信息", description = "根据终端IP获取终端信息")
    @GetMapping("/getSopInfo/{ip}")
    public Result<?> getSopInfo(@PathVariable("ip") String ip) {
        Map<String,Object> sopInfo = sopDocumentManageService.getSopInfo(ip);
        return Result.success(sopInfo);
    }
}
